//
//  PickerAlertView.m
//  YunPlus
//
//  Created by juxue.chen on 8/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PickerAlertView.h"

@implementation PickerAlertView

@synthesize datePickerView;

static UIColor *fillColor = nil;
static UIColor *borderColor = nil;

NSArray *pickerData;


- (id)init
{
    
    NSDictionary *dict1 = @{@"name_payment" : @"Наличными",@"name_payment_db":@"cash"};
    NSDictionary *dict2 = @{@"name_payment" : @"Кредитной картой",@"name_payment_db":@"roboxchange"};
    NSDictionary *dict3 = @{@"name_payment" : @"По счету",@"name_payment_db":@"schet"};
    NSDictionary *dict4 = @{@"name_payment" : @"Наложенный платеж",@"name_payment_db":@"nalojka"};
    NSDictionary *dict5 = @{@"name_payment" : @"Курьеру при получении",@"name_payment_db":@"cod"};
    
    pickerData = @[dict1,dict2,dict3,dict4,dict5];
    
    
    
    
    
    
    
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.4];
    
    return self;
}



+ (void) setBackgroundColor:(UIColor *) background
            withStrokeColor:(UIColor *) stroke
{
    if(fillColor != nil)
    {
     }
    
    fillColor = background;
    borderColor = stroke;
}




- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        if(fillColor == nil)
        {
            fillColor = [UIColor blackColor];
            borderColor = [UIColor colorWithHue:0.625
                                     saturation:0.0 brightness:0.8 alpha:0.8];
        }
        
		[self createDatePicker];
	}
	return self;
}

- (void)setFrame:(CGRect)rect {
	[super setFrame:CGRectMake(0, 0, 320, 300)];//width 默认 284
	self.center = CGPointMake(320/2, 280);
}

- (void)layoutSubviews {
	[super layoutSubviews];
    
    
    for (UIView *sub in [self subviews])
    {
        if([sub class] == [UIImageView class] && sub.tag == 0)
        {
            // The alert background UIImageView tag is 0,
            // if you are adding your own UIImageView's
            // make sure your tags != 0 or this fix
            // will remove your UIImageView's as well!
            [sub removeFromSuperview];
            break;
        }
    }
    
    
	for (UIView *view in self.subviews) {
		if (view.frame.size.height == 43) {
			view.frame = CGRectMake(view.frame.origin.x, 232, 127, 43);
		}
	}
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextClearRect(context, rect);
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetLineWidth(context, 0.0);
    CGContextSetAlpha(context, 0.8);
    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
    CGContextSetFillColorWithColor(context, [fillColor CGColor]);
    
    // Draw background
    CGFloat backOffset = 2;
    CGRect backRect = CGRectMake(rect.origin.x + backOffset,
                                 rect.origin.y + backOffset,
                                 rect.size.width - backOffset*2,
                                 rect.size.height - backOffset*2);
    
    [self drawRoundedRect:backRect inContext:context withRadius:8];
    CGContextDrawPath(context, kCGPathFillStroke);
    
    // Clip Context
    CGRect clipRect = CGRectMake(backRect.origin.x + backOffset-1,
                                 backRect.origin.y + backOffset-1,
                                 backRect.size.width - (backOffset-1)*2,
                                 backRect.size.height - (backOffset-1)*2);
    
    [self drawRoundedRect:clipRect inContext:context withRadius:8];
    CGContextClip (context);
    
    //Draw highlight
    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.35, 1.0, 1.0, 1.0, 0.06 };
    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace,
                                                        components, locations, num_locations);
    
    CGRect ovalRect = CGRectMake(-130, -115, (rect.size.width*2),
                                 rect.size.width/2);
    
    CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
    CGPoint end = CGPointMake(rect.origin.x, rect.size.height/5);
    
    CGContextSetAlpha(context, 1.0);
    CGContextAddEllipseInRect(context, ovalRect);
    CGContextClip (context);
    
    CGContextDrawLinearGradient(context, glossGradient, start, end, 0);
    
    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace);
}

- (void) drawRoundedRect:(CGRect) rrect inContext:(CGContextRef) context
              withRadius:(CGFloat) radius
{
    CGContextBeginPath (context);
    
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect),
    maxy = CGRectGetMaxY(rrect);
    
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    CGContextClosePath(context);
}
#pragma mark -
#pragma mark UIPickerView - Date/Time

- (void)createDatePicker
{
	datePickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
	datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
	datePickerView.frame = CGRectMake(10, 10, 300, 216);//216
	datePickerView.delegate = self;
	datePickerView.showsSelectionIndicator = YES;
	[self addSubview:datePickerView];
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView
numberOfRowsInComponent:(NSInteger)component
{
    [self setDefaultValues];
     return pickerData.count;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [[pickerData objectAtIndex:row] objectForKey:@"name_payment"];
}


- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //NSLog(@"%@",[[pickerData objectAtIndex:row] objectForKey:@"name_payment"]);
    [Singleton sharedMySingleton].text_payment = [[pickerData objectAtIndex:row] objectForKey:@"name_payment"];
    [Singleton sharedMySingleton].text_payment_db = [[pickerData objectAtIndex:row] objectForKey:@"name_payment_db"];
}


-(void)setDefaultValues{
    [Singleton sharedMySingleton].text_payment = [[pickerData objectAtIndex:0] objectForKey:@"name_payment"];
    [Singleton sharedMySingleton].text_payment_db = [[pickerData objectAtIndex:0] objectForKey:@"name_payment_db"];
    //NSLog(@"%@",[[pickerData objectAtIndex:0] objectForKey:@"name_payment"]);
}







@end
